import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;

import pluginUtils.Complexe; 
import pluginUtils.GeneralizedCircle;
import pluginUtils.MobiusTransform;
import pluginUtils.CircleInversion;



public class AAATestInverter extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	private double rayon=1; 
	
	private PrintStream output; 
	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Green","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	private static double altitude=10; 
	private static int number=0; 
	
	private void drawColoredCircle(Circle c,int i){
		double xCenter=c.getCenter().getReal(); 
		double yCenter=c.getCenter().getIm(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
		primeIP.setColor(palette1[i]); 
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
		//if(c.getRadius()>5) return ; 
		/*
		String s="sphere{\n<"+xCenter+","+yCenter+",0>,";
		s+=c.getRadius(); 
		s+=" texture{pigment {color "+palette[i]+" filter 0.5}} finish {reflection 0.5 specular 0.7} }"; 
		//output.println(s);
		 */
		  
		number++; 
		
		double Xs=c.getCenter().getReal(); 
		double Ys=c.getCenter().getIm(); 
		double rapp=Xs*Xs+Ys*Ys+1; 
		double u=Xs/rapp; 
		double v=Ys/rapp; 
		double w=(Xs*Xs+Ys*Ys)/rapp;
		
		double Xsr=Xs; 
		double Ysr=Ys+c.getRadius(); 
		double rappr=Xsr*Xsr+Ysr*Ysr+1; 
		double ur=Xsr/rappr; 
		double vr=Ysr/rappr; 
		double wr=(Xsr*Xsr+Ysr*Ysr)/rappr;
		
		double nrad=(u-ur)*(u-ur)+(v-vr)*(v-vr)+(w-wr)*(w-wr);
		nrad=Math.sqrt(nrad)*1.414; 
		
		String s="sphere{<"+u+","+v+","+w+">,";
		//String s="sphere{<"+c.getXcenter()*X+",0,"+c.getYcenter()*Y+">,";
		//s+=c.getRadius()*X;
		s+=nrad; 
		s+=" texture{Texture"+i+"}finish{Finish"+i+"}}"; 
		//output.println(s);
		
	}
	
	private void drawColoredGeneralizedCircle(GeneralizedCircle c,int i){
		if(c.getGca()!=0){
			if(c.determinant()<0){
				double radius=Math.sqrt(c.getGcb().squareNorm()/(c.getGca()*c.getGca())-c.getGcd()/c.getGca());
				Complexe center=Complexe.mulbyDouble(c.getGcc(),-1/c.getGca());
				drawColoredCircle(new Circle(center,radius), i); 
				return; 
			}// if determinant
			if(c.determinant()==0){
				drawPoint(Complexe.mulbyDouble(c.getGcc(),-1/c.getGca())); 
				return; 
			}// det==0
		}// gca!=0
		if(c.getGca()==0){
			double b1=c.getGcb().getReal(); 
			double b2=c.getGcb().getIm(); 
			if(b2!=0)
				drawLine(x0,b1*x0/b2+c.getGcd()/(2*b2),x1,b1*x1/b2+c.getGcd()/(2*b2));
			else
				drawLine(-c.getGcd()/2*b1,y0,-c.getGcd()/2*b1,y1);
		}// gca==0
	
	}

	/*
	public String toString(){
		String s=gca+","+gcb+","+gcc+","+gcd+"\n"; 
		if(gca!=0){
			if(this.determinant()<0){
				s+="Real Circle \n";
				double radius=Math.sqrt(gcb.squareNorm()/(gca*gca)-gcd/gca);
				Complexe center=Complexe.mulbyDouble(gcc,-1/gca); 
				s+="Radius :"+radius+"\n"+"Center :"+center; 
			}
			if(this.determinant()==0){
				s+="Point Circle\n";
				Complexe center=Complexe.mulbyDouble(gcc,-1/gca); 
				s+="Center :"+center; 
			}
			if(this.determinant()>0){
				s+="Imaginary Circle\n";
			}
		}
	if(gca==0){
		s+="Line\n";
		if(gcb.getIm()==0) s+="Vertical \n"; 
			
	}
	return s;
	}
	*/
	
	
	private void drawPoint(Complexe c){
		double xCenter=c.getReal(); 
		double yCenter=c.getIm(); 
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	private void drawLine(Complexe c1,Complexe c2){
		int XD=(int)(c1.getReal()*acoef+ccoef); 
		int YD=(int)(c1.getIm()*vcoef+wcoef); 
		int XF=(int)(c2.getReal()*acoef+ccoef); 
		int YF=(int)(c2.getIm()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		double limit=20; 
	
		setBounds(-limit,limit,-limit,limit); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(5); 
		
	
		
		
	
		
		// Reflector
	
		
		setProcessor("Verifying", primeIP);
		
		drawLine(x0,0,x1,0);
		drawLine(0,y0,0,y1);
		
		
		Circle seed=new Circle(Complexe.createComplexeFromRI(1,1),3); 
		
		
			drawColoredCircle(seed,3); 
			
		System.out.println(seed); 
		
		
		
		
	
		
		
		
		
		show();
		System.out.println("done "+number); 
		
		
	}	
}
	